/*--------------------------------------------------------------------------*
 *                               Driver
 *                              PFC15163
 *--------------------------------------------------------------------------*/

#define _RTC_MOUDEL_
#include "rtc85163.h"
#include "iic.h"

void RtcStart()
{
	uint8_t Data;
	Data = 0;
	
	ISendStr(RTC_CHIP_ADDR, RTC_SUBA_ADDR_CON_1, &Data, 1);
}

void RtcStop()
{
	uint8_t Data;
	Data = CON_1_STOP_MASK;
	ISendStr(RTC_CHIP_ADDR, RTC_SUBA_ADDR_CON_1, &Data, 1);
}

void RtcGetTime(TIME_Type *RtcTime)
{
	uint8_t Data[7];
	IRcvStr(RTC_CHIP_ADDR, RTC_SUBA_ADDR_TIME_SEC, Data, 7);
	RtcTime->Sec   = ( ((Data[0] & 0x70) >> 4 ) *10) + (Data[0] & 0x0F); 
	RtcTime->Min   = ( ((Data[1] & 0x70) >> 4 ) *10) + (Data[1] & 0x0F); 
	RtcTime->Hour  = ( ((Data[2] & 0x30) >> 4 ) *10) + (Data[2] & 0x0F); 
	RtcTime->Day   = ( ((Data[3] & 0x30) >> 4 ) *10) + (Data[3] & 0x0F);
    RtcTime->Week  = ( ((Data[4] & 0x30) >> 4 ) *10) + (Data[4] & 0x0F);
	RtcTime->Mon   = ( ((Data[5] & 0x10) >> 4 ) *10) + (Data[5] & 0x0F);
	RtcTime->Year  = ( ((Data[6] & 0xF0) >> 4 ) *10) + (Data[6] & 0x0F);
	RtcTime->Year += CenturyCur;
}

void RtcSetTime(TIME_Type *RtcTime)
{
	uint8_t Data[8];
	uint8_t Cal;
	Data[0] = ((RtcTime->Sec  / 10) << 4) + (RtcTime->Sec  % 10);
    Data[1] = ((RtcTime->Min  / 10) << 4) + (RtcTime->Min  % 10);
	Data[2] = ((RtcTime->Hour / 10) << 4) + (RtcTime->Hour % 10);
	Data[3] = ((RtcTime->Day  / 10) << 4) + (RtcTime->Day  % 10);
	Data[4] = RtcTime->Week & 0x07;
	Data[5] = ((RtcTime->Mon  / 10) << 4) + (RtcTime->Mon  % 10);
	Cal     = RtcTime->Year % 100;
	Data[6] = ((Cal / 10) << 4) + (Cal % 10);
    ISendStr(RTC_CHIP_ADDR, RTC_SUBA_ADDR_TIME_SEC, Data, 7);
}

